package com.example.design.item.composite;

import lombok.*;

import javax.swing.text.AbstractDocument;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


//组合模式 Composite 树枝构件
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ProductComposite extends AbstractProductItem{
    private int id;
    private int pid;
    private String name;
    private List<AbstractProductItem> child = new ArrayList<>();


    //新增商品类目
    @Override
    public void add(AbstractProductItem abstractProductItem) {
        this.child.add(abstractProductItem);
    }


    ////删除商品类目
    @Override
    public void remove(AbstractProductItem abstractProductItem) {
        ProductComposite productItem = (ProductComposite) abstractProductItem;
        Iterator<AbstractProductItem> iterator = child.iterator();
        while (iterator.hasNext()){
            ProductComposite next = (ProductComposite)iterator.next();
            //移除商品类目
            if (next.getId() == productItem.getId()){
                iterator.remove();
                break;
            }
        }
    }
}
